package cn.iocoder.yudao.module.cms.appversion.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cms.appversion.dal.dataobject.AppVersionDO;
import cn.iocoder.yudao.module.cms.appversion.service.AppVersionService;
import cn.iocoder.yudao.module.cms.appversion.vo.AppVersionRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.api.cms.ErrorCodeConstants.APP_VERSION_NOT_EXISTS;

@Tag(name = "用户 App - 版本更新")
@RestController
@RequestMapping("/cms/app-version")
@Validated
public class AppAppVersionController {

    @Resource
    private AppVersionService appVersionService;

    @GetMapping("/latest")
    @Operation(summary = "获得最新版本信息")
    @Parameter(name = "type", description = "类型：0-UNIAPP、1-安卓APP", example = "0")
    @PermitAll
    public CommonResult<AppVersionRespVO> getLatestAppVersion(@RequestParam(value = "type") Integer type) {
        AppVersionDO appVersion = appVersionService.getLatestAppVersion(type);
        if (appVersion == null) {
            return error(APP_VERSION_NOT_EXISTS);
        }
        return success(BeanUtils.toBean(appVersion, AppVersionRespVO.class));
    }

}
